/*******************************************************************************
 * 文件名: ApplicationPropUtils.java
 * 描    述: 应用程序参数类
 * 创建人: Dean Huang(hudi-112@163.com)
 * 日　期: 2018-08-04
 * 描　述: 文件描述
 * 版　本: 1.0
 *******************************************************************************/

package com.z1.core.utils;

import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

public class ApplicationPropUtils {

	private static Properties props = new Properties();
	
	static {
		reload();
	}

	private static void reload() {
		InputStream input = null;
		try {
			input = ApplicationPropUtils.class.getResourceAsStream("/application.properties");
			props.clear();
			props.load(input);
		}
		catch (Throwable e) {
			e.printStackTrace();
		}
		finally {
			if (null != input) {
				try {
					input.close();
					input = null;
				}
				catch (Exception e) {}
			}
		}
	}
	
	public static String getProperty(String key) {
		if (props.containsKey(key)) {
			Object obj = props.getProperty(key);
			if (null != obj) {
				return obj.toString();
			}
		}
		return null;
	}
	public static String getProperty(String key, String def) {
		if (props.containsKey(key)) {
			Object obj = props.getProperty(key);
			if (null != obj) {
				return obj.toString();
			}
		}
		return def;
	}
	
	public static Map<String, String> getProps() {
		Map<String, String> map = new HashMap<String, String>();
		for (Object key : props.keySet()) {
			map.put(key.toString(), props.get(key).toString());
		}
		return map;
	}
	
	public static Map<String, String> getRegexProps(String regex) {
		Map<String, String> regexMap = new HashMap<String, String>();
		
		Map<String, String> map = getProps();
		for (String key : map.keySet()) {
			if (key.toString().matches(regex)) {
				regexMap.put(key.toString(), map.get(key));
			}
		}
		
		return regexMap;
	}
	
	public static boolean containsKey(String key) {
		return props.containsKey(key);
	}
	
}
